perm filename PART13.TEX[TEX,RWF]3 blob sn#541599 filedate 1980-10-15 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00003 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	\sendnotes{Indefinite iteration section}
C00004 00003	\sample
C00007 ENDMK
C⊗;
\sendnotes{Indefinite iteration section}

\exercise
Write a PASCAL program to find four positive integers
$$A < B < C < D$$
for which $B↑3 + C↑3 = A↑3 + D↑3$.  Another way of stating this problem
is:  find a positive integer that can be represented two ways as the sum of
two cubes.  You may find it useful to look for the smallest such integer, but
any number you find satisfying the above condition is acceptable.  Your program 
should print this integer as well as its two distinct representations.

Hint:  let the outer iteration control $D$ .

\endexercise
\sample
\example
\sendnotes{indefinite iteration or sample program section}

\startcode
PROGRAM SUMOFCUBES ( OUTPUT ) ;

(* THIS PROGRAM FINDS FOUR INTEGERS: 0 < A < B < C < D
   FOR WHICH   A*A*A + D*D*D = B*B*B + C*C*C  *)

VAR     A, B, C, D :  INTEGER ;

BEGIN
A := 1 ;
B := 2 ;
C := 3 ;
D := 4 ;

WHILE  A*A*A + D*D*D <> B*B*B +C*C*C DO
    IF A < B - 1  THEN  A := A + 1
    ELSE
        BEGIN
        A := 1 ;
        IF B < C - 1  THEN  B := B + 1
        ELSE 
            BEGIN
            B := 2 ;
            IF C < D + 1  THEN  C := C + 1
            ELSE
                BEGIN
                C := 3 ;
                D := D + 1 
                END
            END
        END ;
WRITELN ( A, D, B, C )
END.
\endcode

The program generates values for {\tt A, B, C, D} in the order:

\dtable{\hfill#\hfill\qquad⊗\hfill#\hfill\qquad⊗\hfill#\hfill\qquad⊗\hfill#\hfill\cr
A⊗B⊗C⊗D\cr
1⊗2⊗3⊗4\cr
1⊗2⊗3⊗5\cr
1⊗2⊗4⊗5\cr
1⊗3⊗4⊗5\cr
2⊗3⊗4⊗5\cr
1⊗2⊗3⊗6\cr
1⊗2⊗4⊗6\cr
1⊗3⊗4⊗6\cr
2⊗3⊗4⊗6\cr
1⊗2⊗5⊗6\cr
1⊗3⊗5⊗6\cr
2⊗3⊗5⊗6\cr
1⊗4⊗5⊗6\cr
2⊗4⊗5⊗6\cr
3⊗4⊗5⊗6\cr
1⊗2⊗3⊗7\cr
⊗etc.⊗⊗\cr
}%end dtable

The program, upon finding $A↑3 + D↑3 ≠ B↑3 + C↑3$, increases the first of
{\tt A, B, C, D} that can be increased without becoming equal to the next
variable; it sets all the earlier variables back to their initial values.